package com.ibeeking.found.cop.b.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.cop.b.rest.service.IFriendsLifeCyclePhaseService;
import com.ibeeking.found.cop.service.common.dos.FriendsLifeCyclePhaseDO;
import com.ibeeking.found.cop.service.common.query.FriendsLifeCyclePhaseQuery;
import com.ibeeking.found.cop.service.common.vo.FriendsLifeCyclePhaseVO;
import com.ibeeking.found.cop.service.mapper.FriendsLifeCyclePhaseMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName FriendsLifeCyclePhaseServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class FriendsLifeCyclePhaseServiceImpl extends ServiceImpl<FriendsLifeCyclePhaseMapper, FriendsLifeCyclePhaseDO> implements IFriendsLifeCyclePhaseService {

    @Resource
    private FriendsLifeCyclePhaseMapper friendsLifeCyclePhaseMapper;

    @Override
    public List<FriendsLifeCyclePhaseVO> list(FriendsLifeCyclePhaseQuery query) {
        List<FriendsLifeCyclePhaseDO> entityDOS = friendsLifeCyclePhaseMapper.selectList(new LambdaQueryWrapper<FriendsLifeCyclePhaseDO>()
                .eq(query.getId() != null, FriendsLifeCyclePhaseDO::getId, query.getId())
                .eq(query.getCycleId() != null, FriendsLifeCyclePhaseDO::getCycleId, query.getCycleId())
                .like(StringUtils.isNotBlank(query.getName()), FriendsLifeCyclePhaseDO::getName, query.getName())
                .orderByDesc(FriendsLifeCyclePhaseDO::getCreateTime)
        );
        List<FriendsLifeCyclePhaseVO> entityVOS =JsonUtils.jsonToList(entityDOS, FriendsLifeCyclePhaseVO.class);
        return entityVOS;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateBatch(List<FriendsLifeCyclePhaseDO> entityDOS) {
        return super.updateBatchById(entityDOS);
    }

    @Override
    public FriendsLifeCyclePhaseVO view(Long id) {
        FriendsLifeCyclePhaseDO entityDO = friendsLifeCyclePhaseMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        FriendsLifeCyclePhaseVO entityVO = BeanUtil.convertBean(entityDO, FriendsLifeCyclePhaseVO.class);
        return entityVO;
    }

}
